import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'


// 路由权限白名单
const allowList = ['adminLogin', 'login', 'register', 'portal', 'search', 'detail', '403', '404',
  '/admin/login', '/admin/register', '/userLogin', '/captcha', '/webHeader','/']
// 前台登录地址
const loginRoutePath = '/index/userLogin'
// 后台登录地址
const adminLoginRoutePath = '/adminLogin'



const routes = [
  {
    path: '/',
    name: 'home',
    // redirect: '/index',
    component: HomeView
  },
  {
    path: '/content',
    name:'content',
    component: ()=>import('../views/index/components/content_r.vue'),
  },
  {
    path: '/index',
    name: 'index',
    redirect: '/index/protal',
    component: ()=>import('../views/index.vue'),
    children:[
      {
        path: 'protal',
        name: 'protal',
        component: ()=>import('../views/index/user/protal.vue')
      },
      {
        path: 'userLogin',
        name: 'userLogin',
        component: ()=>import('../views/index/user/userLogin.vue')
      }
    ]
  },
  {
    path: '/admin/login',
    name: 'adminLogin',
    component: () => import('../views/admin/adminLogin.vue')
  },
  {
    path: '/admin/register',
    name: 'adminRegister',
    component: () => import('../views/admin/adminRegister.vue')
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('../views/admin/JobManagement.vue'),
    children: [

      {
        path: 'thing',
        name: 'thing',
        component: () => import('../views/admin/thing.vue')
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('../views/admin/user.vue')
      },
      {
        path: 'tag',
        name: 'tag',
        component:()=>import('../views/admin/tag.vue')
      },
      {
        path: 'classification',
        name: 'classification',
        component:()=>import('../views/admin/classification.vue')
      },
      {
        path: 'comment',
        name: 'comment',
        component:()=>import('../views/admin/comment.vue')
      },
      {
        path: 'resume',
        name: 'resume',
        component:()=>import('../views/admin/resume.vue')
      },
      {
        path: 'notice',
        name: 'notice',
        component:()=>import('../views/admin/notice.vue')
      },
      {
        path: 'sysInfo',
        name: 'sysInfo',
        component:()=>import('../views/admin/sysInfo.vue')
      },
      {
        path: 'overview',
        name: 'overview',
        component:()=>import('../views/admin/overview.vue')
      },
      {
        path: 'LoginLog',
        name: 'LoginLog',
        component:()=>import('../views/admin/loginLog.vue')
      },
      {
        path: 'opLog',
        name: 'opLog',
        component:()=>import('../views/admin/opLog.vue')
      },
      {
        path: 'errorLog',
        name: 'errorLog',
        component:()=>import('../views/admin/errorLog.vue')
      }
    ]
  },
  {
    path: '/captcha',
    name: 'captcha',
    component: () => import('../components/Captcha.vue')
  },
  {
    path: '/webHeader',
    name: 'webHeader',
    component: () => import('../views/index/components/webHeader.vue')
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

function isAuthenticated(to) {
  let tokenName = to.path.startsWith('/admin') ? 'admin_user_token' : 'User_token';
  return localStorage.getItem(tokenName) !== null;
}
// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  const allowList = ['/admin/login', '/admin/register', 'index/userLogin', '/captcha', '/webHeader','/']

  // 检查目标路由是否在白名单内
  if (allowList.includes(to.path)) {
    next(); // 在白名单内，直接放行
  } else {
    // 不在白名单内，检查登录状态
    if (isAuthenticated(to)) {
      next(); // 已登录，允许访问
    } else {
      // 未登录，重定向到登录页
      if (to.path.startsWith('/admin') && !isAuthenticated(to)) {
        next('/admin/login'); // 只有未认证时才重定向到后台登录页
      } else if (!allowList.includes(to.path) && !isAuthenticated(to)) {
        next(); // 对于不在白名单内的其他路径，重定向到前台登录页
      } else {
        next(); // 其他情况直接放行
      }
    }
  }
});
export default router
